package com.shenzhen.horselive.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.CountDownTimer;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatButton;

import com.shenzhen.horselive.R;


public class CountDownButton extends AppCompatButton {


    private String afterText = "重发";
    private int ms = 10000;

    public CountDownButton(Context context) {
        super(context);
    }

    public CountDownButton(Context context, AttributeSet attrs) {

        this(context, attrs, 0);

    }

    public CountDownButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.timerbutton);


        afterText = typedArray.getString(R.styleable.timerbutton_afterText);

        ms = typedArray.getInt(R.styleable.timerbutton_ms, 10000);

        typedArray.recycle();

    }

    public void init(String afterText, int ms) {
        this.afterText = afterText;
        this.ms = ms;
    }

    public void startTimer() {
        CountDownButton.this.setTextColor(getResources().getColor(R.color.color_9));
        CountDownButton.this.setEnabled(false);
        new CountDownTimer(ms + 1000, 1000) {

            @Override
            public void onTick(long finish) {
                CountDownButton.this.setText("(" + finish / 1000 + ")" +"重新获取");
            }

            @Override
            public void onFinish() {
                CountDownButton.this.setEnabled(true);
                CountDownButton.this.setText(afterText);
                CountDownButton.this.setTextColor(getResources().getColor(R.color.black));
            }
        }.start();

    }
}
